<?php
	if(isset($_POST['add'])){
		$ch = curl_init();

		$url = "https://www.google.com/accounts/ClientLogin";

		$data =	array (
						'accountType' 	=> 'GOOGLE',
						'Email' 		=> $_POST[user], 
						'Passwd' 		=> $_POST[pass],
						'source'		=> 'weCare',
						'service'		=> 'health'
				);

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		$auth = curl_exec($ch);
		$auth = explode("=", $auth);
		$auth = $auth[3];

		//
		// Lista de Perfiles
		//

		$url = "https://www.google.com/health/feeds/profile/list";
		$header[] = 'Authorization: GoogleLogin auth=' . $auth;

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POST, false);

		$profilelist = curl_exec($ch);

		$xmlDoc = new DOMDocument();
		$xmlDoc->loadXML($profilelist);
		
		$xpath = new DOMXPath($xmlDoc);
		$xpath->registerNamespace('atm', "http://www.w3.org/2005/Atom");
		
		
		
		$profileid = $xpath->query("//atm:entry[atm:title='".$_POST[pname]."']/atm:content")->item(0)->nodeValue;
		if ($profileid == '') {
			echo '<script language="javascript">alert("Profile name is not correct");</script>';
		}else{
			$sql = "SELECT * FROM foreignaccounts WHERE patientID = " . $patientID . " AND type = '" . $_POST['acctype'] . "' AND username = '".$_POST['user']."' AND argument = '".$_POST['pname']."'" ; 
			$result = mysql_query($sql); 
			if (mysql_num_rows($result) > 0) {
				//checar que no exista el nombre de usuario
				echo '<script language="javascript">alert("Connection Already Exist With That Account"); window.location = "index.php?mode=newghaccount&id='.$patientID.'";</script>';
			} else {
				$sql = "INSERT INTO foreignaccounts (patientID, type, username, password, argument, lastUpdated) VALUES ('".$patientID."', '".$_POST['acctype']."', '".$_POST['user']."','".$_POST['pass']."','".$_POST['pname']."','1970-01-01')";
				$result = mysql_query($sql); 
				echo '<script language="javascript">alert("Account Added"); window.location = "index.php?mode=newghaccount&id='.$patientID.'";</script>';
			}
		}
	} else {

		$sql = "SELECT * FROM foreignaccounts WHERE patientID = ".$patientID;
		$result = mysql_query($sql);
	
		if (mysql_num_rows($result) > 0) {
			echo "<table width=80% border=0 class=\"tabla1\">\n";
			echo "<tr><th colspan=9>Accounts</th></tr>\n";
			echo "<tr><th>Type</th><th>Username</th><th>Profile name</th><th>Last updated</th><th>Import</th><th>Delete</th></tr>\n";
			while ($row = mysql_fetch_assoc($result)) {
				echo "<tr><td>{$row['type']}</td><td><a href='?mode=editghaccount&accid={$row['id']}'>{$row['username']}</a></td><td>{$row['argument']}</td><td>{$row['lastUpdated']}</td><td><a href='?mode=importgh&accid={$row['id']}'>Import</a></td><td><a href='?mode=deleteitem&itemid={$row['id']}&itemtype=ForeignAccount'>Delete</a></td></tr>\n";
			}
			echo "</table><br \>\n";
		}

		?>
			
		  <form action="index.php?mode=newghaccount&id=<? echo $patientID; ?>" method="post">
			<input type="hidden" name="acctype" value="GoogleHealth" />
			<table width=80% border=0 class="tabla1">
			  <tr><th colspan=2>Add an account</th></tr>
			  <tr>
				<td><label for="name">Username:</label></td>
				<td><input type="text" class="reqd" id="user" name="user"/></td>
			  </tr>
			  <tr>
				<td><label for="password">Password:</label></td>
				<td><input type="password" class="reqd" id="pass" name="pass"/></td>
			  </tr>
			  <tr>
				<td><label for="name">Perfil Name:</label></td>
				<td><input type="text" class="reqd" id="pname" name="pname"/></td>
			  </tr>
			</table>
			<table class="center">
			  <tr>
				<td><input type="submit" value="Add" name="add" id="add" class="boton"/></td>
				<td><input type="reset" value="Cancel" class="boton" onclick="resetForm();"/></td>
			  </tr>
			</table>
		 </form>
	<?php
	}
	?>